/*
 *  Copyright 2016 Google Inc. All Rights Reserved.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.google.blockly.ohos.ui.fieldview;

import com.google.blockly.model.Field;
import com.google.blockly.model.FieldDate;
import com.google.blockly.ohos.ResourceTable;
import com.google.blockly.utils.ImageUtils;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.render.Canvas;
import ohos.agp.utils.Rect;
import ohos.app.Context;

import java.text.DateFormat;

/**
 * Renders a date and a date picker as part of a Block.
 */
public class BasicFieldDateView extends Text implements FieldView {
    private static final DateFormat LOCALIZED_FORMAT = DateFormat.getDateInstance(DateFormat.SHORT);

    protected Field.Observer mFieldObserver = new Field.Observer() {
        @Override
        public void onValueChanged(Field field, String oldValue, String newValue) {
            String dateStr = LOCALIZED_FORMAT.format(mDateField.getDate());
            if (!dateStr.contentEquals(getText())) {
                setText(dateStr);
            }
        }
    };

    private final DrawTask mDrawTask = new DrawTask() {
        @Override
        public void onDraw(Component component, Canvas canvas) {
            Element bg = ImageUtils.getPatchDrawable(getContext(), ResourceTable.Media_edit_text_background_disabled);
            if (bg != null) {
                bg.setBounds(new Rect(0, 0, component.getEstimatedWidth(), component.getEstimatedHeight()));
                bg.drawToCanvas(canvas);
            }
        }
    };

    protected FieldDate mDateField;

    public BasicFieldDateView(Context context) {
        super(context);
    }

    public BasicFieldDateView(Context context, AttrSet attrs) {
        super(context, attrs);
        setBackground(null);
        addTextObserver(new TextObserver() {
            @Override
            public void onTextUpdated(String s, int i, int i1, int i2) {
                if (mDateField != null) {
                    mDateField.setFromString(s);
                }
            }
        });
        addDrawTask(mDrawTask);
    }

    @Override
    public void setField(Field field) {
        FieldDate dateField = (FieldDate) field;
        if (mDateField == dateField) {
            return;
        }

        if (mDateField != null) {
            mDateField.unregisterObserver(mFieldObserver);
        }
        mDateField = dateField;
        if (mDateField != null) {
            setText(mDateField.getLocalizedDateString());
            mDateField.registerObserver(mFieldObserver);
        } else {
            setText("");
        }
    }

    @Override
    public Field getField() {
        return mDateField;
    }

    @Override
    public void unlinkField() {
        setField(null);
    }
}
